package com.showcai.service.impl;

import com.showcai.dao.UserAccountMapper;
import com.showcai.module.UserAccount;
import com.showcai.module.UserAccountLog;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.service.ServiceBase;
import com.showcai.service.UserAccountLogService;
import com.showcai.service.UserAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Baggio on 2018-12-12.
 */
@Service
public class UserAccountServiceImpl extends ServiceBase implements UserAccountService {
    @Autowired
    UserAccountMapper userAccountMapper;
    @Autowired
    UserAccountLogService userAccountLogService;
    /**
     * 账户默认余额
     */
    private static final int USER_ACCOUNT_DEFAULT_BALANCE = 0;

    private UserAccount createUserAccount(Long userId, Byte accountType) {
        Integer balance = USER_ACCOUNT_DEFAULT_BALANCE;

        UserAccount userAccount = userAccountMapper.getUserAccount(new HashMap<String, String>() {{
            put("userId", String.valueOf(userId));
            put("accountType", String.valueOf(accountType));
        }});
        if (userAccount != null) {
            return userAccount;
        }

        userAccount = new UserAccount();
        userAccount.setUserId(userId);
        userAccount.setBalance(balance);
        userAccount.setAccountType(accountType);
        userAccount.setCreateTime(new Date());
        userAccount.setStatus(UserAccount.STATUS_VALID);
        userAccountMapper.insertUserAccount(userAccount);
        return userAccount;

    }

    private void updateUserAccount(UserAccount userAccount) {
        userAccountMapper.updateUserAccountByPrimaryKeySelective(userAccount);
    }


    private Integer getAfterBalance(Byte operateType, Integer balance, Integer beforeBalance) {

        Integer afterBalance = (operateType == UserAccountLog.OPERATE_TYPE_PAY ? beforeBalance - balance : beforeBalance + balance);
        if(afterBalance<0){
            throw new MyRunTimeExcption("余额不足！");
        }
        return afterBalance;

    }

    @Transactional
    @Override
    public void modifyUserAccount(Long userId, Byte accountType, Integer balance, String remark, Byte operateType) throws MyRunTimeExcption {
        if (userId == null) {
            throw new MyRunTimeExcption("userId is null");
        }
        if (accountType == null) {
            throw new MyRunTimeExcption("accountType is null");
        }
        if (balance == null) {
            throw new MyRunTimeExcption("balance is null");
        }
        if (operateType == null) {
            throw new MyRunTimeExcption("operateType is null");
        }


        UserAccount userAccount = createUserAccount(userId, accountType);


        UserAccountLog userAccountLog = new UserAccountLog();
        userAccountLog.setBalance(balance);
        userAccountLog.setCreateTime(new Date());
        userAccountLog.setAccountType(accountType);
        userAccountLog.setUserId(userId);
        userAccountLog.setBeforeBalance(userAccount.getBalance());
        userAccountLog.setAfterBalance(getAfterBalance(operateType, balance, userAccount.getBalance()));
        userAccountLog.setOperateType(operateType);
        userAccountLog.setRemark(remark);
        userAccountLogService.addUserAccountLog(userAccountLog);

        userAccount.setBalance(userAccountLog.getAfterBalance());
        userAccount.setCreateTime(new Date());
        userAccount.setUserId(userId);
        userAccount.setAccountType(accountType);
        userAccount.setBalance(userAccountLog.getAfterBalance());

        userAccountMapper.updateUserAccountByPrimaryKeySelective(userAccount);


    }

    @Override
    public List<UserAccount> getUserAccountListByUserId(Long userId) throws MyRunTimeExcption {
        return null;
    }
}
